﻿Imports GBDZafiro

Public Class FrmUndMed

    Private Sub FrmUndMed_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cargargvUniMed()
        cbFraccion.Text = "NO"
    End Sub

    Protected Overrides Sub bnNuevo_Click(sender As System.Object, e As System.EventArgs)
        MyBase.bnNuevo_Click(sender, e)
        tbUnidad.Focus()
    End Sub

    Protected Overrides Sub bNGuardar_Click(sender As System.Object, e As System.EventArgs)
        If Not validarCampos() Then
            Exit Sub
        End If
        updateAddUndMed()
        MyBase.bnGuardar_Click(sender, e)
        cargargvUniMed()
    End Sub

    Protected Overrides Sub bnEliminar_Click(sender As System.Object, e As System.EventArgs)
        If msnBox.msnQuestion("¿Desea borrar la Unidad de Medida " + tbUnidad.Text + " ?") = _
            Windows.Forms.DialogResult.Yes Then
            Dim undMed As UnidadMedida = crearUndMed()
            UnidadMedida.Eliminar(undMed)
            cargargvUniMed()
            msnBox.msnInformacion("Unidad de Medida Borrada")
            MyBase.bnEliminar_Click(sender, e)
        End If
    End Sub

    Private Sub gvUndMed_RowHeaderMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gvUndMed.RowHeaderMouseClick
        selectUniMed()
    End Sub

    Private Sub gvUndMed_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles gvUndMed.KeyDown
        'selectUniMed()
    End Sub

    Private Sub gvUndMed_CurrentCellChanged(sender As System.Object, e As System.EventArgs) Handles gvUndMed.CurrentCellChanged
        selectUniMed()
    End Sub

    Private Sub selectUniMed()
        Try
            Dim rowgvUndMed As New DataGridViewRow()
            rowgvUndMed = gvUndMed.CurrentRow
            tbUnidad.Text = rowgvUndMed.Cells.Item("ID_UNIDAD_MEDIDA").Value.ToString()
            tbDescripcion.Text = rowgvUndMed.Cells.Item("DESCRIPCION").Value.ToString()

            If rowgvUndMed.Cells.Item("FRACCION").Value.ToString() = "S" Then
                cbFraccion.Text = "SI"
            Else
                cbFraccion.Text = "NO"
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cargargvUniMed()
        gvUndMed.DataSource = Nothing
        gvUndMed.Rows.Clear()
        gvUndMed.DataSource = UnidadMedida.ConsultarAll()

        With gvUndMed
            .Columns(0).HeaderText = "UNIDAD"
        End With
    End Sub

    Private Function crearUndMed() As UnidadMedida
        Dim undMed As New UnidadMedida
        undMed.idUndMed = tbUnidad.Text.ToUpper()
        undMed.descripcion = tbDescripcion.Text.ToUpper()

        If cbFraccion.Text = "SI" Then
            undMed.fraccion = "S"
        Else
            undMed.fraccion = "N"
        End If
        Return undMed
    End Function

    Private Function validarCampos() As Boolean
        If tbDescripcion.TextLength < 1 Or tbUnidad.TextLength < 1 Or _
            cbFraccion.Text.Length < 1 Then
            msnBox.msnExclamacion("Complete todos los campos")
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub updateAddUndMed()
        Dim uniMed As UnidadMedida = crearUndMed()
        If UnidadMedida.exisUndMed(uniMed) Then
            UnidadMedida.Actualizar(uniMed)
            'msnBox.msnInformacion("Unidad de Medida Actualizada")
        Else
            UnidadMedida.Nuevo(uniMed)
            'msnBox.msnInformacion("Unidad de Medida Guardada")
        End If
    End Sub
   
End Class
